package com.sanjiayi.controller;

import com.sanjiayi.pojo.Comment;
import com.sanjiayi.pojo.Follow;
import com.sanjiayi.result.Result;
import com.sanjiayi.result.QueryPageBean;
import com.sanjiayi.result.PageResult;
import com.sanjiayi.service.CommentService;
import com.sanjiayi.service.FollowService;
import com.sanjiayi.service.LikeService;
import com.sanjiayi.service.MomentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = "社交功能接口")
@RestController
@RequestMapping("/social")
public class SocialController {

    @Autowired
    private LikeService likeService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private FollowService followService;

    @Autowired
    private MomentService momentService;

    @ApiOperation("点赞/取消点赞")
    @PostMapping("/like/{momentId}")
    public Result<Map<String, Object>> toggleLike(@PathVariable Integer momentId, @RequestParam Integer userId) {
        boolean isLiked = likeService.toggleLike(userId, momentId);
        int likeCount = likeService.getLikeCount(momentId);
        return Result.ok(Map.of(
            "isLiked", isLiked,
            "likeCount", likeCount
        ));
    }

    @ApiOperation("检查是否已点赞")
    @GetMapping("/like/check/{momentId}")
    public Result<Boolean> checkLike(@PathVariable Integer momentId, @RequestParam Integer userId) {
        boolean hasLiked = likeService.hasLiked(userId, momentId);
        return Result.ok(hasLiked);
    }

    @ApiOperation("获取动态点赞数")
    @GetMapping("/like/count/{momentId}")
    public Result<Integer> getLikeCount(@PathVariable Integer momentId) {
        int likeCount = likeService.getLikeCount(momentId);
        return Result.ok(likeCount);
    }

    @ApiOperation("发表评论")
    @PostMapping("/comment")
    public Result<Map<String, Object>> addComment(
            @RequestParam(value = "momentId", required = true) String momentId,
            @RequestParam(value = "userId", required = true) String userId,
            @RequestParam(value = "content", required = true) String content) {
        try {
            // 参数校验
            if (momentId == null || momentId.trim().isEmpty()) {
                return Result.error("动态ID不能为空");
            }
            if (userId == null || userId.trim().isEmpty()) {
                return Result.error("用户ID不能为空");
            }
            if (content == null || content.trim().isEmpty()) {
                return Result.error("评论内容不能为空");
            }

            // 转换参数
            Integer momentIdInt;
            Integer userIdInt;
            try {
                momentIdInt = Integer.parseInt(momentId.trim());
                userIdInt = Integer.parseInt(userId.trim());
            } catch (NumberFormatException e) {
                return Result.error("ID格式不正确");
            }
            
            Comment comment = new Comment();
            comment.setMomentId(momentIdInt);
            comment.setUserId(userIdInt);
            comment.setContent(content.trim());
            comment.setCreateTime(new Date());
            
            Comment savedComment = commentService.addComment(comment);
            int commentCount = commentService.getCommentCount(momentIdInt);
            
            return Result.ok(Map.of(
                "comment", savedComment,
                "commentCount", commentCount
            ));
        } catch (Exception e) {
            return Result.error("评论失败：" + e.getMessage());
        }
    }

    @ApiOperation("获取动态评论列表")
    @GetMapping("/comment/{momentId}")
    public Result<Map<String, Object>> getComments(@PathVariable Integer momentId) {
        List<Comment> comments = commentService.getCommentsByMomentId(momentId);
        int commentCount = commentService.getCommentCount(momentId);
        return Result.ok(Map.of(
            "comments", comments,
            "commentCount", commentCount
        ));
    }

    @ApiOperation("获取动态评论数")
    @GetMapping("/comment/count/{momentId}")
    public Result<Integer> getCommentCount(@PathVariable Integer momentId) {
        int commentCount = commentService.getCommentCount(momentId);
        return Result.ok(commentCount);
    }

    @ApiOperation("关注/取消关注用户")
    @PostMapping("/follow/{followedId}")
    public Result<Map<String, Object>> toggleFollow(@PathVariable Integer followedId, @RequestParam Integer followerId) {
        boolean isFollowed = followService.toggleFollow(followerId, followedId);
        int followerCount = followService.getFollowerCount(followedId);
        int followingCount = followService.getFollowingCount(followerId);
        return Result.ok(Map.of(
            "isFollowed", isFollowed,
            "followerCount", followerCount,
            "followingCount", followingCount
        ));
    }

    @ApiOperation("检查是否已关注")
    @GetMapping("/follow/check/{followedId}")
    public Result<Boolean> checkFollow(@PathVariable Integer followedId, @RequestParam Integer followerId) {
        boolean hasFollowed = followService.hasFollowed(followerId, followedId);
        return Result.ok(hasFollowed);
    }

    @ApiOperation("获取用户的关注列表")
    @GetMapping("/follow/following/{userId}")
    public Result<Map<String, Object>> getFollowingList(@PathVariable Integer userId) {
        List<Follow> followingList = followService.getFollowingList(userId);
        int followingCount = followService.getFollowingCount(userId);
        return Result.ok(Map.of(
            "followingList", followingList,
            "followingCount", followingCount
        ));
    }

    @ApiOperation("获取用户的粉丝列表")
    @GetMapping("/follow/followers/{userId}")
    public Result<Map<String, Object>> getFollowerList(@PathVariable Integer userId) {
        List<Follow> followerList = followService.getFollowerList(userId);
        int followerCount = followService.getFollowerCount(userId);
        return Result.ok(Map.of(
            "followerList", followerList,
            "followerCount", followerCount
        ));
    }

    @ApiOperation("获取用户的关注数量")
    @GetMapping("/follow/following/count/{userId}")
    public Result<Integer> getFollowingCount(@PathVariable Integer userId) {
        int followingCount = followService.getFollowingCount(userId);
        return Result.ok(followingCount);
    }

    @ApiOperation("获取用户的粉丝数量")
    @GetMapping("/follow/followers/count/{userId}")
    public Result<Integer> getFollowerCount(@PathVariable Integer userId) {
        int followerCount = followService.getFollowerCount(userId);
        return Result.ok(followerCount);
    }

    @ApiOperation("获取关注用户的动态列表")
    @GetMapping("/following/moments")
    public Result<PageResult> getFollowingMoments(
            @RequestParam Integer userId,
            @RequestParam(required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        try {
            QueryPageBean queryPageBean = new QueryPageBean();
            queryPageBean.setCurrentPage(currentPage);
            queryPageBean.setPageSize(pageSize);
            
            PageResult pageResult = momentService.getFollowingMoments(userId, queryPageBean);
            return Result.ok(pageResult);
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage() );
        } catch (Exception e) {
            return Result.error("获取关注用户动态失败：" + e.getMessage());
        }
    }
} 